//
//  StructViewController.swift
//  Swift4.0_Learn
//
//  Created by lantaiyuan on 2017/9/29.
//  Copyright © 2017年 lantaiyuan. All rights reserved.
//

import UIKit
//定义两个结构体
struct Point {
    var x = 0.0,y = 0.0
}
struct Size {
    var width = 0.0,height = 0.0
}
// MARK ---结构体本身也是对象
struct Rect {
    var origin = Point()
    var size = Size()
    
    func getCenter() -> (Double,Double) {
        let x = origin.x + (size.width / 2)
        let y = origin.y + (size.height / 2)
        return (x,y)
    }
    var center: Point {
        let x = origin.x + (size.width / 2)
        let y = origin.y + (size.height / 2)
        return Point.init(x: x, y: y)
    }
    var center2: Point {
        get {
            let x = origin.x + (size.width / 2)
            let y = origin.y + (size.height / 2)
            return Point.init(x: x, y: y)
        }
        set {
            origin.x = newValue.x - (size.width / 2)
            origin.y = newValue.y - (size.height / 2)
        }
    }
}
// MARK ---协议
protocol sydProtocol:NSObjectProtocol {
    func compare(value:Int) -> Bool
}

class StructViewController: UIViewController,sydProtocol {
   //延迟加载
    lazy var rect = Rect.init(origin: Point.init(x: 100, y: 100), size: Size.init(width: 200, height: 100))

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
        //super.init(coder: aDecoder)
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        view.backgroundColor = UIColor.white
        self.title = "Struct"
        let (x,y) = rect.getCenter()
        print("\(x),\(y)")
        let p2 = rect.center
        print("\(p2.x),\(p2.y)")
        let p3 = Point.init(x: 300, y: 200)
        rect.center2 = p3
        print("\(rect.origin.x),\(rect.origin.y)")
    }
    // MARK ---sydProtocol
    func compare(value: Int) -> Bool {
        return true
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
